package com.tkjt.pms.impt.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.impt.ICMirrorImporterService;
import com.tkjt.pms.impt.common.KpiConsts;
import com.tkjt.pms.impt.common.KpiImportRepositories;
import com.tkjt.pms.impt.vos.CIndexVo;
import com.tkjt.pms.impt.vos.KpiIndexDataVo;
import com.tkjt.pms.impt.vos.KpiIndexValueVo;

import com.tkjt.pms.modules.common.mapper.TkIndexValueMapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;
import java.util.Set;
@Slf4j
@Service
@Transactional
public class CMirrorImporter extends ServiceImpl<TkIndexValueMapper, TkIndexValue> implements ICMirrorImporterService {


	@Autowired
	private KpiMirrorUpdater kpiMirrorUpdater;
    @Override
	public void doImporter(String mirrorCode, String filePath) {

		ImportParams params = new ImportParams();
		params.setTitleRows(1);
		params.setHeadRows(2);
		params.setNeedSave(false);

		File file = new File(filePath);
        if(!file.exists()){
            return;
        }
		List<CIndexVo> results = ExcelImportUtil.importExcel(file, CIndexVo.class, params);
		log.info("results size=" + results.size());

		/**************************************************************************
		 * 处理权重
		 */
		this.processIndexWeightData(mirrorCode, results);

		/**************************************************************************
		 * 处理指标值
		 */
		this.processIndexValueData(mirrorCode, results);

	}

	/**************************************************************************
	 * 处理权重
	 */
    private void processIndexWeightData(final String mirrorCode, List<CIndexVo> results) {
		log.info("unit,start index weight...");
		for (CIndexVo indexVo : results) {
			String indexCode = indexVo.getCode();
			log.info("index weight, mirrorCode=" + mirrorCode + "index code=" + indexCode);
			{
				// 运营社区
				String valueStr = indexVo.getW01();
				Set<String> orgs = KpiImportRepositories.groupOrgs.get("运营社区");
				for (String orgName : orgs) {
					kpiMirrorUpdater.updateIndexWeightMirror(mirrorCode, KpiConsts.INDEX_GROUP_C, indexCode, orgName, valueStr);
				}
			}
			{
				// 筹开社区
				String valueStr = indexVo.getW02();
				Set<String> orgs = KpiImportRepositories.groupOrgs.get("筹开社区");
				for (String orgName : orgs) {
					kpiMirrorUpdater.updateIndexWeightMirror(mirrorCode, KpiConsts.INDEX_GROUP_C, indexCode, orgName, valueStr);
				}
			}
			{
				// 燕申医院
				String valueStr = indexVo.getW03();
				Set<String> orgs = KpiImportRepositories.groupOrgs.get("燕申医院");
				for (String orgName : orgs) {
					kpiMirrorUpdater.updateIndexWeightMirror(mirrorCode, KpiConsts.INDEX_GROUP_C, indexCode, orgName, valueStr);
				}
			}
			{
				// 粤蜀医院
				String valueStr = indexVo.getW04();
				Set<String> orgs = KpiImportRepositories.groupOrgs.get("粤蜀医院");
				for (String orgName : orgs) {
					kpiMirrorUpdater.updateIndexWeightMirror(mirrorCode, KpiConsts.INDEX_GROUP_C, indexCode, orgName, valueStr);
				}
			}
			{
				// 吴楚医院
				String valueStr = indexVo.getW05();
				Set<String> orgs = KpiImportRepositories.groupOrgs.get("吴楚医院");
				for (String orgName : orgs) {
					kpiMirrorUpdater.updateIndexWeightMirror(mirrorCode, KpiConsts.INDEX_GROUP_C, indexCode, orgName, valueStr);
				}
			}
			{
				// 社区合计
				String valueStr = "0.0";
				kpiMirrorUpdater.updateIndexWeightMirror(mirrorCode, KpiConsts.INDEX_GROUP_C, indexCode, "社区合计", valueStr);
				// 医院合计
				valueStr = "0.0";
				kpiMirrorUpdater.updateIndexWeightMirror(mirrorCode, KpiConsts.INDEX_GROUP_C, indexCode, "医院合计", valueStr);
				// 园区合计
				valueStr = "0.0";
				kpiMirrorUpdater.updateIndexWeightMirror(mirrorCode, KpiConsts.INDEX_GROUP_C, indexCode, "园区合计", valueStr);
			}
		}
		log.info("unit,end index weight.");
	}

	/**************************************************************************
	 * 处理指标值
	 */
    private void processIndexValueData(String mirrorCode, List<CIndexVo> results) {
		log.info("unit,start index value...");
		for (CIndexVo indexVo : results) {
			String indexCode = indexVo.getCode();
			log.info("start index value, indexCode=" + indexCode);
			List<KpiIndexValueVo> indexValues = indexVo.getIndexValues();
			{
				// 燕园社区
				List<KpiIndexDataVo> indexData = indexVo.getIndexData1();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "燕园社区", indexData, indexValues);
			}
			{
				// 燕园医院
				List<KpiIndexDataVo> indexData = indexVo.getIndexData2();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "燕园医院", indexData, indexValues);
			}
			{
				// 申园社区
				List<KpiIndexDataVo> indexData = indexVo.getIndexData3();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "申园社区", indexData, indexValues);
			}
			{
				// 申园医院
				List<KpiIndexDataVo> indexData = indexVo.getIndexData4();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "申园医院", indexData, indexValues);
			}
			{
				// 粤园社区
				List<KpiIndexDataVo> indexData = indexVo.getIndexData5();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "粤园社区", indexData, indexValues);
			}
			{
				// 粤园医院
				List<KpiIndexDataVo> indexData = indexVo.getIndexData6();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "粤园医院", indexData, indexValues);
			}
			{
				// 蜀园社区
				List<KpiIndexDataVo> indexData = indexVo.getIndexData7();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "蜀园社区", indexData, indexValues);
			}
			{
				// 蜀园医院
				List<KpiIndexDataVo> indexData = indexVo.getIndexData8();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "蜀园医院", indexData, indexValues);
			}
			{
				// 吴园社区
				List<KpiIndexDataVo> indexData = indexVo.getIndexData9();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "吴园社区", indexData, indexValues);
			}
			{
				// 吴园医院
				List<KpiIndexDataVo> indexData = indexVo.getIndexData10();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "吴园医院", indexData, indexValues);
			}
			{
				// 楚园社区
				List<KpiIndexDataVo> indexData = indexVo.getIndexData11();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "楚园社区", indexData, indexValues);
			}
			{
				// 楚园医院
				List<KpiIndexDataVo> indexData = indexVo.getIndexData12();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "楚园医院", indexData, indexValues);
			}
			{
				// 社区合计
				List<KpiIndexDataVo> indexData = indexVo.getIndexData13();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "社区合计", indexData, indexValues);
			}
			{
				// 医院合计
				List<KpiIndexDataVo> indexData = indexVo.getIndexData14();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "医院合计", indexData, indexValues);
			}
			{
				// 园区合计
				List<KpiIndexDataVo> indexData = indexVo.getIndexData15();
				kpiMirrorUpdater.updateIndexValueDataMirror(mirrorCode, indexCode, "园区合计", indexData, indexValues);
			}
			log.info("unit,end index value.");
		}
	}

}
